package com.sls.web.manager.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.sys.pojo.Permission;
import com.whalin.MemCached.MemCachedClient;

@SuppressWarnings("unchecked")
public class MainAction extends SuperAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = -1570442984220960806L;

	public String main() {
		return "main";
	}

	@Resource(name = "memcachedClient")
	private MemCachedClient memCachedClient;
	List<Permission> permissionList;
	private String serviceCode;

	public String getServiceCode() {
		return serviceCode;
	}

	public void setServiceCode(String serviceCode) {
		this.serviceCode = serviceCode;
	}

	public List<Permission> getPermissionList() {
		return permissionList;
	}

	public void setPermissionList(List<Permission> permissionList) {
		this.permissionList = permissionList;
	}

	// 后台Header

	public String header() {
		String menu1 = Constants.MENU1;
		Object ob = memCachedClient.get(menu1);
		if (ob instanceof HashMap) {
			List<String> list = CommonUtil.gson().fromJson(
					this.getCurrentOperator().getServiceCode(),
					new TypeToken<List<String>>() {
					}.getType());
			Map<String, Permission> map = (HashMap<String, Permission>) ob;
			if (permissionList == null) {
				permissionList = new ArrayList<Permission>();
			}
			if (list != null) {
				for (String co : list) {
					Permission p = map.get(co);
					if (p != null)
						permissionList.add(p);
				}
			}
		}
		if (permissionList!=null&&permissionList.size()>1) {
			Collections.sort(permissionList, new Comparator<Permission>() {
				@Override
				public int compare(Permission o1, Permission o2) {
					if (o1.getSort()!=null&&o2.getSort()!=null) {
						int i = o1.getSort() - o2.getSort();
						if (i!=0) {
							return i;
						}
					}
						return Integer.parseInt(o1.getCode())-Integer.parseInt(o2.getCode());
				}
			});
		}
		return "header";
	}

	// 后台左边菜单
	public String menu() {
		List<String> codeList = CommonUtil.gson().fromJson(
				this.getCurrentOperator().getServiceCode(),
				new TypeToken<List<String>>() {
				}.getType());
		if (codeList.size() > 0) {
			List<Permission> ob = this.getMenuLv2(serviceCode, codeList);
			if (ob instanceof ArrayList) {
				List<Permission> retList = (ArrayList<Permission>) ob;
				for (Permission p : retList) {
					if (permissionList == null)
						permissionList = new ArrayList<Permission>();
					if (p != null)
						permissionList.add(p);
				}
			}
		}
		return "menu";
	}

	// 后台首页
	public String index() {
		return "index";
	}

	// 后台中间(显示/隐藏菜单)
	public String middle() {
		return "middle";
	}

	public List<Permission> getMenuLv2(String code, List<String> codeList) {
		List<Permission> list = new ArrayList<Permission>();
		Object ob = memCachedClient.get(Constants.MENU2);
		if (ob instanceof ArrayList) {
			ArrayList<Permission> list2 = (ArrayList<Permission>) ob;
			for (Permission p : list2) {
				if (p != null && p.getParentCode() != null
						&& p.getParentCode().equals(code)
						&& codeList.contains(p.getCode())) {
					p.setList(getMenuLv3(p.getCode(), codeList));
					list.add(p);
				}
			}
		}
		Collections.sort(list, new Comparator<Permission>() {
			@Override
			public int compare(Permission o1, Permission o2) {
				if (o1.getSort()!=null&&o2.getSort()!=null) {
					int i = o1.getSort() - o2.getSort();
					if (i!=0) {
						return i;
					}
				}
					return Integer.parseInt(o1.getCode())-Integer.parseInt(o2.getCode());
			}
		});
		return list;
	}

	public <T> List<Permission> getMenuLv3(String code, List<String> codeList) {
		List<Permission> list = new ArrayList<Permission>();
		Object ob = memCachedClient.get(Constants.MENU3);
		if (ob instanceof ArrayList) {
			ArrayList<Permission> list3 = (ArrayList<Permission>) ob;
			for (Permission p : list3) {
				if (p != null && p.getParentCode() != null
						&& p.getParentCode().equals(code)
						&& codeList.contains(p.getCode())) {
					list.add(p);
				}
			}
		}
		Collections.sort(list, new Comparator<Permission>() {
			@Override
			public int compare(Permission o1, Permission o2) {
				if (o1.getSort()!=null&&o2.getSort()!=null) {
					int i = o1.getSort() - o2.getSort();
					if (i!=0) {
						return i;
					}
				}
					return Integer.parseInt(o1.getCode())-Integer.parseInt(o2.getCode());
			}
		});
		return list;
	}
}
